package com.cabinet.web.controller;

import java.io.File;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import com.cabinet.web.entity.Seller;

import cn.com.cabinet.service.SellerServiceImpl;

@Controller
@RequestMapping("/seller/application")
public class ApplicationController {
	
	@Autowired
	private SellerServiceImpl sellerServiceImpl;
	
	@RequestMapping()
	public String redirectApplication(){
		return "/homePage/application";
	}
	
	@RequestMapping("/submit")
	public String applicationSubmit(MultipartFile ICardFile,String name,String phone,String Icode,
			String mail,String nr,ModelMap modelMap){
		System.out.println(mail);
		Seller se = sellerServiceImpl.getSellerBysPhone(phone);
		if(se == null){
			ApplicationDto apdto = new ApplicationDto();
			apdto.setEmail(mail);
			apdto.setIcode(Icode);
			apdto.setName(name);
			apdto.setNr(nr);
			apdto.setPhone(phone);
			String fileName= uploadResume(ICardFile,apdto);
			
			Seller s = new Seller();
			s.setNr(apdto.getNr());
			s.setsEmail(apdto.getEmail());
			s.setsICard(apdto.getIcode());
			s.setsMark(0);
			s.setsPhone(apdto.getPhone());
			s.setsPhoto("upload/cabinet/identity/img/"+fileName);
			s.setsTrueName(apdto.getName());
			sellerServiceImpl.saveSeller(s);
			
			modelMap.addAttribute("message", 1);
		}else{
			modelMap.addAttribute("message", 2);//用户已提交过申请
		}
		return "/homePage/application";
		
	}
	
	
		public class ApplicationDto{
			private String name;
			private String phone;
			private String Icode;
			//private String ICardFile;
			private String email;
			private String nr;
			public String getName() {
				return name;
			}
			public void setName(String name) {
				this.name = name;
			}
			public String getPhone() {
				return phone;
			}
			public void setPhone(String phone) {
				this.phone = phone;
			}
			public String getIcode() {
				return Icode;
			}
			public void setIcode(String icode) {
				Icode = icode;
			}
			public String getEmail() {
				return email;
			}
			public void setEmail(String email) {
				this.email = email;
			}
			public String getNr() {
				return nr;
			}
			public void setNr(String nr) {
				this.nr = nr;
			}
			
		}
		
		
		/**
		 * 用户上传文件
		 */
		public String uploadResume(MultipartFile resumeFile,ApplicationDto appDto){
			

			if(resumeFile!=null){
		   		// 原始文件名称
		        String resumeFile_name =  resumeFile.getOriginalFilename();
		        if(resumeFile_name!=null && resumeFile_name.trim().length()>0){
		            String newFileName =  appDto.getIcode() + resumeFile_name.substring(resumeFile_name.lastIndexOf("."));
		            //上传图片  上传到当前项目路径下的uppload文件夹下
		            String path = "/opt/upload/cabinet/identity/img";
		            File uploadPic = new File(path+ "/" +newFileName);
		            if(!uploadPic.exists()){
		                uploadPic.mkdirs();
		            }
		            
		            //向磁盘写文件
		            try {
		            	resumeFile.transferTo(uploadPic);
					} catch (Exception e) {
						e.printStackTrace();
					}
		            return newFileName;
		          
		        }
			}
				return null;
		}

}

